/* 
 * File:   main.c
 * Author: FENG Xu <fengxu1989@gmail.com>
 *
 * Created on May 29, 2012, 4:51 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../h/es.h"

#define BR false
#define MSB true

FILE *bit_stream_in;
FILE *bit_stream_out;
binio_t in, out;

/*
 * Jeu de test
 */
int main(int argc, char ** argv) {
	char* _help="help";
	char* _C="-C";
	char* _D="-D";
	binio_error_t code;
	bool br=BR;
	bool msb=MSB;

	if(!strcmp(argv[1],_help) && argc==2){
		help();
	}
	else{
		if(argc!=4){
			printf("Erreur: nombre de parametres.\n");
			help();
		}
		else{
			code = binio_init (2);
			fprintf (stderr,"code retour init= %d\n",code);
			
			if(!strcmp(argv[1],_C)){
				bit_stream_in=fopen(argv[2],"r");
				bit_stream_out=fopen(argv[3],"w");
						
				if(bit_stream_in==NULL) printf("Erreur: lecture.\n");
				if(bit_stream_out==NULL) printf("Erreur: ecriture.\n");
				/*
				in=binio_read_open((binio_fgetc_t *)fgetc, 
									(binio_eof_t *)feof, 
									(binio_file_func_param_t) bit_stream_in,  
									msb);
				*/
				out=binio_write_open ((binio_fputc_t *)fputc, 
									(binio_eof_t *)feof, 
									(binio_file_func_param_t)bit_stream_out,
									msb);
				if(out!=NULL) printf("Ouverture ok\n");
				
				compresser(bit_stream_in, out);
				binio_close(out);
				//binio_close(in);
				fclose(bit_stream_out);
				fclose(bit_stream_in);
				printf("Compression terminee.\n");
			}
			else if(!strcmp(argv[1],_D)){
				/*
				bit_stream_in=fopen(argv[2],"r");
				bit_stream_out=fopen(argv[3],"w");
				
				if(bit_stream_in==NULL) printf("Erreur: lecture.\n");
				if(bit_stream_out==NULL) printf("Erreur: ecriture.\n");
				
				in=binio_read_open((binio_fgetc_t) fgetc, 
									(binio_eof_t)feof, 
									(binio_file_func_param_t) bit_stream_in,  
									br);
				out=binio_write_open ((binio_fgetc_t)fputc, 
									(binio_eof_t)feof, 
									(binio_file_func_param_t)bit_stream_out,
									br);
				
				ouvrir (msb);
				fprintf (stderr, "Ouverture ok\n");
				fprintf (stderr, "\n");

				ecrire (br);
				fprintf (stderr,"\n");
				lire (br);
				fprintf (stderr,"\n");
				
				decompresser(in, bit_stream_out);
				binio_close(out);
				binio_close(in);
				fclose(bit_stream_out);
				fclose(bit_stream_in);
				printf("Decompression terminee.\n");
				*/
			}
			else{
				printf("Erreur: parametres.\n");
				help();
			}
		}
	}
	return 0;
}

